package vn.outsource.weather.ui;

import java.util.ArrayList;
import java.util.List;

import vn.tdx.androidlibs.ui.BaseFragmentActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.example.com.outsource.weather.R;
import com.viewpagerindicator.CirclePageIndicator;
import com.viewpagerindicator.IconPageIndicator;
import com.viewpagerindicator.IconPagerAdapter;

public class MainActivity extends BaseFragmentActivity implements OnClickListener,OnPageChangeListener{
	private final String TAG = "MainActivity";

	private ViewPager mViewPager;
	private CirclePageIndicator  mCirclePageIndicator;
	
	private WeatherFragmentAdapter mWeatherFragmentAdapter;
	private ImageView mimgLocation;
	private ImageView mimgCalendar;
	private ImageView mimgRadar;
	private View mMainLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		
		SettingFragment settingFragment=SettingFragment.newInstance();
		WeatherFragment weatherFragment=WeatherFragment.newInstance();
		WeatherFragment weatherFragment1=WeatherFragment.newInstance();
		WeatherFragment weatherFragment2=WeatherFragment.newInstance();
		
		List<Fragment> fragments=new ArrayList<Fragment>();
		fragments.add(settingFragment);
		fragments.add(weatherFragment);
		fragments.add(weatherFragment1);
		fragments.add(weatherFragment2);
		mWeatherFragmentAdapter=new WeatherFragmentAdapter(getSupportFragmentManager(), fragments);
		mViewPager.setAdapter(mWeatherFragmentAdapter);
		mCirclePageIndicator.setViewPager(mViewPager,2);
	}
	
	private void initView(){
		mViewPager=(ViewPager)findViewById(R.id.activity_main_viewpager);
		mimgCalendar=(ImageView)findViewById(R.id.activity_main_img_calendar);
		mimgLocation=(ImageView)findViewById(R.id.activity_main_img_location);
		mimgRadar=(ImageView)findViewById(R.id.activity_main_img_radar);
		mCirclePageIndicator=(CirclePageIndicator)findViewById(R.id.activity_main_indicator);
		mMainLayout=findViewById(R.id.activity_main_layout);
		
		mimgCalendar.setOnClickListener(this);
		mimgLocation.setOnClickListener(this);
		mimgRadar.setOnClickListener(this);
		mCirclePageIndicator.setOnPageChangeListener(this);
	}
	
	private class WeatherFragmentAdapter extends FragmentStatePagerAdapter implements IconPagerAdapter{
		 protected final int[] ICONS = new int[] {
	            R.drawable.perm_group_calendar,
	            R.drawable.perm_group_camera,
	            R.drawable.perm_group_device_alarms,
	            R.drawable.perm_group_location
	    };
		private List<Fragment> mListFragment;
		public WeatherFragmentAdapter(FragmentManager fm,List<Fragment>list) {
			super(fm);
			mListFragment=list;
		}

		@Override
		public Fragment getItem(int arg0) {
			return mListFragment.get(arg0);
		}

		@Override
		public int getCount() {
			return mListFragment.size();
		}

		@Override
		public int getIconResId(int index) {
		   return ICONS[index % ICONS.length];
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.activity_main_img_calendar:
			final Intent intent=new Intent(this,CalendarActivity.class);
			startActivity(intent);
			break;
		case R.id.activity_main_img_location:
			final Intent i=new Intent(this,LocationActivity.class);
			startActivity(i);
			break;
		case R.id.activity_main_img_radar:
			final Intent j=new Intent(this,RadarActivity.class);
			startActivity(j);
			break;

		default:
			break;
		}
	}

	@Override
	protected void setContentView(Bundle bundle) {
		
	}
	@Override
	protected boolean isNavigationEnable() {
		return false;
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int arg0) {
		Fragment fragment=mWeatherFragmentAdapter.getItem(arg0);
		if(fragment instanceof SettingFragment){
			mMainLayout.setBackgroundResource(R.color.black);
			mimgRadar.setEnabled(false);
		}else{
			mMainLayout.setBackgroundResource(R.drawable.bg_morning_hot);
			mimgRadar.setEnabled(true);
		}
	}

}
